{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成功连接到服务器\n"
     ]
    }
   ],
   "source": [
    "# 客户端\n",
    "import socket\n",
    "import sys\n",
    "\n",
    "# 创建 socket 对象\n",
    "s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) \n",
    "\n",
    "# 获取本地主机名\n",
    "host = socket.gethostname() \n",
    "\n",
    "# 设置端口号\n",
    "port = 9999\n",
    "\n",
    "# 连接服务，指定主机和端口\n",
    "s.connect((host, port))\n",
    "\n",
    "# 接收小于 1024 字节的数据\n",
    "bufsize = 1024\n",
    "msg = s.recv(bufsize)\n",
    "\n",
    "s.close()\n",
    "cod = 'utf-8'\n",
    "print (msg.decode(cod))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
